
package newpackage;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.effect.Lighting;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class CriaCadastro extends Principal {

    final PacienteDAO pacienteDAO = new PacienteDAO();    
        Label lbTitulo = new Label("Novo Paciente");
    public GridPane CriaGrid() {
        
        Label lbID = new Label("ID:");
        Label lbNome = new Label("Nome:");
        Label lbEndereco = new Label("Endereço:");
        Label lbCep = new Label("CEP:");
        Label lbCidade = new Label("Cidade:");
        Label lbTelefone = new Label("Telefone:");
        Label lbEstado = new Label("Estado:");
        lbID.setMinWidth(180);
        
        final TextField tbNome = new TextField();
        tbNome.setMinWidth(200);
        final TextField tbEndereco = new TextField();
        final TextField tbCep = new TextField();
        final TextField tbCidade = new TextField();
        final TextField tbTelefone = new TextField();        
        final ChoiceBox cbEstado = new ChoiceBox();
        cbEstado.getItems().addAll("AC", "AL", "AP", "AM", "BA", "CE", "DF", "ES", "GO", "MA", "MT", "MS", "MG",
                "PA", "PB", "PR", "PE", "PI", "RJ", "RN", "RS", "RO", "RR", "SC", "SP", "SE", "TO");
        
        
        
        Button btInserir = new Button("Inserir");
        btInserir.setStyle("-fx-base: blue;");
        
        btInserir.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent t) {
                Paciente p = new Paciente();
                p.setNome(tbNome.getText());
                p.setEndereco(tbEndereco.getText());
                p.setCep(tbCep.getText());
                p.setCidade(tbCidade.getText());
                p.setTelefone(tbTelefone.getText());
                if (!cbEstado.getSelectionModel().isEmpty()) {
                    p.setEstado(cbEstado.getSelectionModel().getSelectedItem().toString());
                }
                pacienteDAO.save(p);
                
                tbNome.setText("");
                tbEndereco.setText("");
                tbCep.setText("");
                tbCidade.setText("");
                tbTelefone.setText("");
                if (!cbEstado.getSelectionModel().isEmpty()) {
                    cbEstado.getSelectionModel().clearSelection();
                }
                final Stage stage = new Stage();
                javafx.scene.Group rootGroup = new javafx.scene.Group();
                Scene scene = new Scene(rootGroup, 350, 100);
                stage.setScene(scene);
                stage.centerOnScreen();
                stage.show();
                Text text = new Text("Cadastrado com Sucesso!");
                text.setFill(Color.DODGERBLUE);
                text.setEffect(new Lighting());
                text.setFont(Font.font(Font.getDefault().getFamily(), 30));
                Button ok = new Button();
                ok.setText("OK");
                ok.setMinWidth(80);
                ok.setDefaultButton(true);
                VBox vBox3 = new VBox();
                vBox3.setSpacing(20);
                vBox3.getChildren().addAll(text, ok);
                vBox3.setAlignment(Pos.CENTER);
                BorderPane msg = new BorderPane();
                msg.setCenter(vBox3);
                rootGroup.getChildren().add(msg);
                ok.setOnAction(new EventHandler<ActionEvent>() {

                    public void handle(ActionEvent event) {
                        stage.hide();                        
                    }
                });
                
            }            
        });
        
        
        Button btLimpar = new Button("Limpar");
        btLimpar.setStyle("-fx-base: blue;");
        
        
        btLimpar.setOnAction(new EventHandler<ActionEvent>() {
            
            @Override
            public void handle(ActionEvent t) {
                tbNome.setText("");
                tbEndereco.setText("");
                tbCep.setText("");
                tbCidade.setText("");
                tbTelefone.setText("");
                if (!cbEstado.getSelectionModel().isEmpty()) {
                    cbEstado.getSelectionModel().clearSelection();
                }
            }
        });
        
        GridPane gp = new GridPane();
        gp.setPadding(new Insets(10, 10, 10, 10));
        gp.setHgap(10);
        gp.setVgap(10);
        gp.add(lbID, 0, 0);
        gp.add(lbNome, 0, 1);
        gp.add(lbEndereco, 0, 2);
        gp.add(lbCep, 0, 3);
        gp.add(lbCidade, 0, 4);
        gp.add(lbTelefone, 0, 5);
        gp.add(lbEstado, 0, 6);
        gp.add(tbNome, 1, 1);
        gp.add(tbEndereco, 1, 2);
        gp.add(tbCep, 1, 3);
        gp.add(tbCidade, 1, 4);
        gp.add(tbTelefone, 1, 5);
        gp.add(cbEstado, 1, 6);
        gp.add(btInserir, 0, 8);
        gp.add(btLimpar, 1, 8);
        
        
        return gp;
    }
}
